// pages/classify/classify.js
const app = getApp()
const url = "https://www.fastmock.site/mock/925fae2205932ec106295492f460736b/hema/getGoods";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList:["休闲零食","中外名酒","水饮冲调","乳品烘培","粮油调味","美妆个护","母婴保健","厨卫百货","日用百货","礼品卡券","新鲜水果","时令蔬菜","肉禽蛋品","海鲜水产"
  ],
    navActiveIndex:0,
    navListContent:{},
    hintState:false,
    hintTime:2000,
    scrollHeight:1050,
    cartList:[]
  },
  purchased:function (e) {
    var cartList = this.data.cartList
    var arr =wx.getStorageSync('cart')||[];
    // console.log("arr,{}", arr);
    // console.log("item", e.currentTarget.dataset.item);
    if(arr.length>0){
      for(var j in arr){
        // console.log("name", arr[j].name);
        if(arr[j].name==e.currentTarget.dataset.item.name){
          // console.log("true");
          arr[j].count = parseInt(arr[j].count)+1
          try {
            wx.setStorageSync('cart', arr)
          } catch (e) {            
            console.log(e)
          }   
          return
        }
      }
      // console.log("不重复");
      arr.push(e.currentTarget.dataset.item)
    }else{
      // console.log("false");
      cartList.push(e.currentTarget.dataset.item)
      arr = cartList;
      // console.log(arr);
    }
    try {
      wx.setStorageSync('cart', arr)
      return;
    } catch (e) {      
      console.log(e)
    }
  },
  hint:function (e) {
    if(this.data.hintState){
      return
    }
    this.purchased(e)
    this.setData({hintState:true})
    wx.showToast({
      title: '成功加入购物车',
      icon: 'success',
      duration: 1500
    })
    setTimeout(()=>{
      this.setData({hintState:false})
    },this.data.hintTime)
  },
  navChange:function (e) {
    this.setData({navActiveIndex:e.currentTarget.dataset.index})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({navActiveIndex:options.id})
    app.getData(url,(data)=>{
      this.setData({navListContent:data.data})
    })
    
    var windowWidth = wx.getSystemInfoSync().windowWidth;
    var windowHeight = wx.getSystemInfoSync().windowHeight;
    var scroll_height = 750*windowHeight/windowWidth-80;
     this.setData({
        scrollHeight:scroll_height
    })  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})